﻿using Controller;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HotelManager
{
    public partial class frmMainPermissionForm : Form
    {

       public bool Updata = false;

        public frmMainPermissionForm()
        {
            InitializeComponent();
            LoadChucVu();
            LoadAllFormSetting();

        }
        
        public void LoadChucVu()
        {
            RoleController ctr = new RoleController();
            dtgrChuVu.DataSource = null;
            dtgrChuVu.DataSource = ctr.GetAllData();
            dtgrChuVu.Columns[2].Visible = false;
            dtgrChuVu.Columns[3].Visible= false;
        }

 

        public void LoadUserAndRole()
        {
            List<TblPermissionsForm> colPerOfUser = new List<TblPermissionsForm>();

            //tap hop danh sach quyen cua user
            DataGridViewSelectedRowCollection row = dtgrChuVu.SelectedRows;

            if (row.Count > 0)
            {
                StorePermission.PositionID = (int)row[0].Cells[0].Value;
               
                List<TblPermissionsForm> list = new List<TblPermissionsForm>();
                list = StorePermission.ColPerOfUser;

                DataTable dt = new DataTable();
                DataRow dr;

                dt.Columns.Add("Id");
                dt.Columns.Add("Form name");

                foreach (TblPermissionsForm item in list)
                {
                    dr = dt.NewRow();

                    dr[0] = (int)item.FormID;
                    dr[1] = (string)item.TblFormSetting.FormName;

                    dt.Rows.Add(dr);    

                }

                dtgrFormSetting.DataSource = null;
                dtgrFormSetting.DataSource = dt;

            }

            

           
        }

        private void dtgrChuVu_SelectionChanged(object sender, EventArgs e)
        {
            LoadUserAndRole();
        }

        private void frmMainPermissionForm_Load(object sender, EventArgs e)
        {
            //dtgrChuVu.ClearSelection();
            //dtgrChuVu.CurrentCell = null; 
        }

        private void btnUpdatePermission_Click(object sender, EventArgs e)
        {
            if (!Updata)
            {
                dtgrFormSetting.Enabled = false;
                dtgrChuVu.Enabled = false;
                dtgvListForm.Enabled = true;
                Updata = true;
                btnUpdatePermission.Text = "Lưu dữ liệu";
                this.BackColor = Color.Red;
            }
            else
            {
                dtgrFormSetting.Enabled = true;
                dtgrChuVu.Enabled = true;
                Updata = false;
                btnUpdatePermission.Text = "Cập nhật phân quyền";
                this.BackColor = Color.SaddleBrown;

                if (dtgrChuVu.SelectedRows.Count>0)
                {
                    PermissionFormController ctr = new PermissionFormController();
                    ctr.DeleteDataByPositionID((int)dtgrChuVu.SelectedRows[0].Cells[0].Value);

                    if (dtgvListForm.SelectedRows.Count>0)
                    {
                        int count = dtgvListForm.SelectedRows.Count;
                        int id = (int)dtgrChuVu.SelectedRows[0].Cells[0].Value;
                        foreach (DataGridViewRow item in dtgvListForm.SelectedRows)
                        {
                            ctr = new PermissionFormController();
                            ctr.objPermission.PositionsID = id;
                            ctr.objPermission.FormID = (int)dtgvListForm.SelectedRows[--count].Cells[0].Value;
                            ctr.AddData();
                           
                        }   
                        
                        LoadUserAndRole(); 
                    }
                    
                }

            }
        }

        private void LoadAllFormSetting() {
            FormSettingController ctr = new FormSettingController();
            dtgvListForm.DataSource = ctr.GetAllData();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            dtgrFormSetting.Enabled = true;
            dtgrChuVu.Enabled = true;
            Updata = false;
            btnUpdatePermission.Text = "Cập nhật phân quyền";
            this.BackColor = Color.SaddleBrown;
        }
    }
}
